/**Jakub Pilch
 * Informatyka IEiT
 * grupa 4, rok I.
 */
package calculate;

import static calculate.Numbers.counter;
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.text.DecimalFormat;

/**Klasa Executors okresla JPanel z przyciskami dzialan prostego
 * kalkuatora oraz zapewnia obsluge tych przyciskow.
 */

class Executors extends JPanel {
    
    JButton plus,minus,div,multi,equal,root, pm;
    DecimalFormat df = new DecimalFormat("#0.00000");//do formatowania double.
    static boolean firstuse = true;
    
    /*Obsluga klikniecia przycisku dzialania*/
    private ActionListener count = new ActionListener(){
        public void actionPerformed(ActionEvent a){
            if(!firstuse)
                equation();
            firstuse = false;
             String txt = ((JButton)a.getSource()).getText();
             Calculate.sig = txt.charAt(0);
             
             if (Calculate.sig == 's'){                 //wyjatek: pierwiastkowanie.
                 if (Calculate.nbr1 >= 0){
                    Calculate.result = Math.sqrt(Calculate.nbr1);
                    Calculate.l1.setText(df.format(Calculate.result));
                    Calculate.nbr1 = Calculate.result;
                    Calculate.nbr2 = 0;
                 }
                 else{
                      JOptionPane.showMessageDialog(new JFrame(),
                                    "Niedozwolona operacja: pierwiastkowanie ujemnej liczby.",
                                    "Blad obliczen", JOptionPane.ERROR_MESSAGE);
                         reset();
                         Calculate.l1.setText(df.format(Calculate.nbr1));
                         return;
                 }
             }
             
             Numbers.eqwait = true;
             Numbers.licznik = 0;
             Numbers.counter = 0.1f;
             Numbers.doted = false;
        }
    };
    
    /*Obsluga klikniecia przycisku '='*/
    private ActionListener equate = new ActionListener(){
        public void actionPerformed(ActionEvent a){
            equation();
            firstuse = true;
        }
    };
    
       /*Oblsuga przycisku zmiany znaku*/    
        private ActionListener plusminus = new ActionListener(){
        public void actionPerformed(ActionEvent a){
            if (Numbers.eqwait){
                Calculate.nbr2 *= -1;
                Calculate.l1.setText(df.format(Calculate.nbr2));
            }
            else{
                Calculate.nbr1 *= -1;
                Calculate.l1.setText(df.format(Calculate.nbr1));
            }
        }
    };

    
    protected Executors(){
        setLayout(new GridLayout(2,1));
        JPanel GrdPan = new JPanel();
        GrdPan.setLayout(new GridLayout(3,2));
        
        plus = new JButton("+");        //inicjalizacja.
        minus = new JButton("-");
        div = new JButton("/");
        multi = new JButton("X");
        equal = new JButton("=");
        root = new JButton ("sqrt");
        pm = new JButton("+/-");
        
        
        plus.addActionListener(count); //dodanie obslugi zdarzen.
        minus.addActionListener(count);
        div.addActionListener(count);
        multi.addActionListener(count);
        equal.addActionListener(equate);
        root.addActionListener(count);
        pm.addActionListener(plusminus);
        
        GrdPan.add(plus);
        GrdPan.add(minus);
        GrdPan.add(div);
        GrdPan.add(multi);
        GrdPan.add(root);
        GrdPan.add(pm);
        add(GrdPan);
        add(equal);
    }
    
    private void equation(){                    //obliczanie wyniku dzialania.
        switch(Calculate.sig){
                case '+':
                    Calculate.result = Calculate.nbr1 + Calculate.nbr2;
                    break;
                case '-':
                    Calculate.result = Calculate.nbr1 - Calculate.nbr2;
                    break;
                case 'X':
                    Calculate.result = Calculate.nbr1 * Calculate.nbr2;
                    break;
                case '/':
                    if (Calculate.nbr2 == 0){   //obsluga dzielenia przez 0.
                        JOptionPane.showMessageDialog(new JFrame(),
                                    "Niedozwolona operacja: dzielenie przez 0.",
                                    "Blad obliczen", JOptionPane.ERROR_MESSAGE);
                         reset();
                         Calculate.l1.setText(df.format(Calculate.nbr1));
                         break;
                    }
                    Calculate.result = Calculate.nbr1 / Calculate.nbr2;
                    break;
                default:
                    Calculate.result = Calculate.nbr1;
                    break;
            }
            Calculate.l1.setText(df.format(Calculate.result));
            Calculate.nbr1 = Calculate.result;
            Calculate.nbr2 = 0;
    }
    
    protected void reset(){
        Calculate.nbr1 = 0;                     //reset danych.
        Calculate.nbr2 = 0;
        Calculate.result = 0;
        counter = 0.1f;
        Calculate.sig='0';
        firstuse = true;
        Numbers.licznik = 0;
        Numbers.doted = false;
        Numbers.eqwait = false;
    }
}
